<template>
  <el-dialog
    title="用户信息"
    v-model="isShow"
    width="50%"
    align-center
  >
    <div class="user-avatar-container">
      <el-image
        :src="user.avatarUrl"
        alt="用户头像"
        class="user-avatar"
        fit="cover"
      />
    </div>

    <el-form
      label-width="120px"
      class="user-form"
    >
      <el-form-item label="用户编号">
        <el-input
          v-model="user.id"
          disabled
        />
      </el-form-item>

      <el-form-item label="用户账号">
        <el-input
          v-model="user.username"
          disabled
        />
      </el-form-item>

      <el-form-item label="用户昵称">
        <el-input
          v-model="user.nickname"
          disabled
        />
      </el-form-item>

      <el-form-item label="用户邮箱">
        <el-input
          v-model="user.email"
          disabled
        />
      </el-form-item>

      <el-form-item label="手机号码">
        <el-input
          v-model="user.mobile"
          disabled
        />
      </el-form-item>

      <el-form-item label="用户性别">
        <el-input
          v-model="user.sexLabel"
          disabled
        />
      </el-form-item>

      <el-form-item label="状态">
        <el-input
          v-model="user.statusLabel"
          disabled
        />
      </el-form-item>

      <el-form-item label="最后登录 IP">
        <el-input
          v-model="user.loginIp"
          disabled
        />
      </el-form-item>

      <el-form-item label="最后登录时间">
        <el-input
          v-model="user.loginDate"
          disabled
        />
      </el-form-item>

      <el-form-item label="创建时间">
        <el-input
          v-model="user.createTime"
          disabled
        />
      </el-form-item>

      <el-form-item label="备注">
        <el-input
          v-model="user.remark"
          type="textarea"
          disabled
        />
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, onMounted, watchEffect } from 'vue'
import { getUserDetail } from '@/api/user'
import dayjs from 'dayjs'

const isShow = defineModel()

const props = defineProps({
  userId: {
    type: Number,
    default: 0
  }
})

const user = reactive({
  id: null,
  username: '',
  nickname: '',
  email: '',
  mobile: '',
  sex: null,
  sexLabel: '',
  avatar: '',
  avatarUrl: '',
  status: null,
  statusLabel: '',
  loginIp: '',
  loginDate: '',
  createTime: '',
  remark: ''
})

const fetchUserData = async () => {
  if (props.userId) {
    const data = await getUserDetail({ id: props.userId })
    Object.assign(user, data)
    user.sexLabel = getSexLabel(data.sex)
    user.statusLabel = getStatusLabel(data.status)
    user.loginDate = formatTime(data.loginDate)
    user.createTime = formatTime(data.createTime)
  }
}

const getSexLabel = sex => {
  const sexEnum = { 1: '男', 2: '女', 3: '未知' }
  return sexEnum[sex] || '未知'
}

const getStatusLabel = status => {
  const statusEnum = { 0: '正常', 1: '禁用' }
  return statusEnum[status] || '未知'
}

const formatTime = time => {
  if (!time) {
    return ''
  }

  return typeof time === 'number' ? dayjs(time).format('YYYY-MM-DD HH:mm:ss') : ''
}

onMounted(() => {
  fetchUserData()
})

watchEffect(() => {
  if (isShow.value) {
    fetchUserData()
  }
})
</script>

<style scoped lang="scss">
.user-avatar-container {
  text-align: center;
  margin-bottom: 20px;

  .user-avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
  }
}

.user-form {
  .el-form-item {
    margin-bottom: 20px;
  }
}
</style>
